﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Equipment.Business" %>
<%@ Import Namespace="Equipment.DataAccess" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Process</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server">woMenu</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
<link rel="stylesheet" type="text/css" href="../../Content/anytime.css" />
    <link rel="stylesheet" type="text/css" href="../../Content/jquery-ui-1.8.16.custom.css" />
    <script type="text/javascript" src="../../Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="../../Scripts/anytime.js"></script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Process</h2>
    <% Dim currWO As WorkOrder = ViewData("CurrWorkOrder")%>
    <table>
        <tr>
            <td>
                 &nbsp;
            </td>
            <td>
                <form id="print" method="post" target="_blank" action='<%= Url.Content("~/Report/WorkOrderDetailReport") %>'>
                <%= Html.Hidden("WorkOrder", currWO.ID)%>
                <input type="submit" class="button right" value="     Print     " />
                </form>
            </td>
        </tr>
    </table>
    <br />
    <% Using Html.BeginForm()%>
    <%= Html.ValidationSummary(False) %>
    <fieldset>
        <legend>Work Order</legend>
        <table class="struct-table">
            <tr>
                <th>
                    Work Order #:
                </th>
                <td>
                    <%: currWO.ID%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Id:
                </th>
                <td>
                    <%: currWO.Asset.AssetID%>
                </td>
                <th>
                    Model #:
                </th>
                <td>
                    <%: currWO.Asset.ModelNo%>
                </td>
            </tr>
            <tr>
                <th>
                    Type of Work Order:
                </th>
                <td>
                    <%= ViewData("RequirementType")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Name:
                </th>
                <td>
                    <%: currWO.Asset.AssetName%>
                </td>
                <th>
                    Serial #:
                </th>
                <td>
                    <%: currWO.Asset.SerialNo%>
                </td>
            </tr>
            <tr>
                <th>
                    Due Date:
                </th>
                <td>
                    <%= currWO.DateDue.ToString(Equipment.Core.Constants.UI.DATE_TIME_FORMAT)%>
               </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Type:
                </th>
                <td>
                    <%: currWO.Asset.AssetType.Description%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th>
                    Work Order Status:
                </th>
                <td>
                    <%: WorkOrderStatus.GetDescriptionByType(currWO.Status)%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Department:
                </th>
                <td>
                    <%: currWO.Asset.SiteDepartment.Description%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th>
                    Additional Details:
                </th>
                <td>
                    <%= Html.TextArea("txtDetails", currWO.Description, New With {.readonly = "readonly", .class = "normal-width"})%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Review Type:
                </th>
                <td>
                    <%=  ViewData("ReviewType")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th class="top">
                    Assigned To:
                </th>
                <td>
                    <%= Html.DropDownList("Groups")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th class="top">
                    Review By Group:
                </th>
                <td>
                    <%= Html.DropDownList("ReviewGroups")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <h4>
                        Monitoring</h4>
                    <br />
                    <%If ViewData("Success_Mess") IsNot Nothing Then%>
                    <p id="status" class="success">
                        <%= ViewData("Success_Mess")%>
                    </p>
                    <%End If%>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>
                                    Measure
                                </th>
                                <th>
                                    Value
                                </th>
                                <th>
                                    Optimal
                                </th>
                                <th>
                                    Additional
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <% For Each measure As WorkOrderMeasure In currWO.Measures%>
                            <tr>
                                <td>
                                    <%= Html.Hidden(String.Format("measureType_{0}", measure.ID), measure.MeasureType.ToString())%>
                                    <%: measure.MeasureDescription%>
                                </td>
                                <%If measure.MeasureDescription = "Comments" Then%>
                                    <td colspan = "3">    
                                <%Else%>
                                    <td>
                                <% End If%>
                                    <% If measure.MeasureType = MeasureType.Text Or measure.MeasureType = MeasureType.Decimal Or measure.MeasureType = MeasureType.Integer Then%>
                                        <%If measure.MeasureDescription = "Comments" Then%>
                                            <%= Html.TextArea(String.Format("measure_{0}", measure.ID), measure.CalibratedValue, New With {.cols = "78", .rows = "5"})%>
                                        <%Else%>
                                            <%= Html.TextBox(String.Format("measure_{0}", measure.ID), measure.CalibratedValue, New With {.class = "text normal-width"})%>
                                        <%End If%>
                                    <%ElseIf measure.MeasureType = MeasureType.Checkbox Then%>
                                    <%  Dim checked As Boolean = False
                                        Dim checkedVal As String = measure.LookupValue.Split(";")(0)
                                        If checkedVal = measure.CalibratedValue Then
                                            checked = True
                                        End If
                                    %>
                                    <%= Html.CheckBox(String.Format("measure_{0}", measure.ID), checked)%>&nbsp;(<%:measure.LookupValue.Split(";")(0)%>/<%: measure.LookupValue.Split(";")(1)%>)
                                    <%ElseIf measure.MeasureType = MeasureType.Dropdown Then%>
                                    <%= Html.DropDownList(String.Format("measure_{0}", measure.ID), New SelectList(measure.LookupValue.Split(";"), measure.CalibratedValue))%>
                                    <%ElseIf measure.MeasureType = MeasureType.Radiobutton Then%>
                                    <% For Each item In measure.LookupValue.Split(";")%>
                                    <%= Html.RadioButton(String.Format("measure_{0}", measure.ID), item, measure.CalibratedValue = item)%>&nbsp;<%:item %>&nbsp;
                                    <%  Next
                                    End If%>
                                </td>
                                <%If measure.MeasureDescription <> "Comments" Then%>
                                <td>
                                    <%If measure.MeasureType = MeasureType.Integer Then%>
                                    <%: measure.IntegerMin%>/<%: measure.IntegerMax%><%= Html.Hidden(String.Format("measureOptMin_{0}", measure.ID), measure.IntegerMin)%><%= Html.Hidden(String.Format("measureOptMax_{0}", measure.ID), measure.IntegerMax)%><%ElseIf measure.MeasureType = MeasureType.Decimal Then%><%: measure.DecimalMin%>/<%: measure.DecimalMax%><%= Html.Hidden(String.Format("measureOptMin_{0}", measure.ID), measure.DecimalMin)%><%= Html.Hidden(String.Format("measureOptMax_{0}", measure.ID), measure.DecimalMax)%><%ElseIf measure.MeasureType = MeasureType.Dropdown Then%><%: measure.OptimalLookupValue%><%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%><%ElseIf measure.MeasureType = MeasureType.Radiobutton Then%><%: measure.OptimalLookupValue%><%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%><%ElseIf measure.MeasureType = MeasureType.Checkbox Then%><%: measure.OptimalLookupValue%><%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%><%= Html.Hidden(String.Format("measureOptOn_{0}", measure.ID), measure.LookupValue.Split(";")(0))%><%= Html.Hidden(String.Format("measureOptOff_{0}", measure.ID), measure.LookupValue.Split(";")(1))%><% End If%>&nbsp;
                                <td>
                                    <% If measure.RequiredComments > 0 Then%>
                                        <%= Html.TextBox(String.Format("measureComment_{0}", measure.ID), measure.MeasureComments, New With {.class = "text normal-width"})%>
                                    <% Else%>
                                        &nbsp;
                                    <%End If%>
                                </td>
                                <td id="errPlace_<%: measure.ID%>" colspan = "3" width="220px">
                                </td>
                                <%End If%>
                            </tr>
                            <% Next%>
                            <tr>
                                <td colspan="7">
                                    <p class="right">
                                        <input id = "bSave" type="submit" class="button" value="Save" onclick="if(validate() === false) return false;" />
                                    </p>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
                <td>
                </td>
            </tr>
        </table>
    </fieldset>
    <% Html.RenderPartial("~/Views/WorkOrder/Dialogs/WorkOrderAuthenticationDialog.ascx")%>
    <% End Using%>
    <div>
        <%: Html.ActionLink("Back to List", Session("BackURL"))%>
    </div>
    <script type="text/javascript">
            var regexpression = new RegExp("^[0-9]+(\.[0-9]{1,2})?$");
            var NOT_IN_RANGE_MESS = "<span class='error'>Value not in the optimal range.</span>";
            function validate() {
                var result = true;

                $('span.error').each(function(i) {
                    $(this).remove();
                });

                var meassIDs = [<%= String.Join(", ", currWO.Measures.Select(Function(m) m.ID))%>];

                for(i = 0; i < meassIDs.length; i++)
                {
                    var id = meassIDs[i];
                    var type = $("#measureType_"+id).val();
                    if(type === "5" || type === "6"){
                        var elVal = $("#measure_"+id).val();
                        if (!elVal.match(regexpression)) {
                            $("#errPlace_" + id).html("<span class='error'>Invalid number.</span>");
                            return false;
                        }
                        else{
                            var optMin = parseFloat($("#measureOptMin_" + id).val());
                            var optMax = parseFloat($("#measureOptMax_" + id).val());
                            var floatVal = parseFloat(elVal);
                            if (optMin > floatVal || floatVal > optMax) {
                                $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                                result = false;
                            }
                        }
                    } else if(type === "1"){
                        var elVal = $("#measure_" + id).val();
                        var optVal = $("#measureOptVal_" + id).val();
                        if (optVal != elVal) {
                            $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }

                    } else if(type === "3"){
                        var elVal = $("#measure_" + id+":checked").val();
                        var optVal = $("#measureOptVal_" + id).val();
                        if (optVal != elVal) {
                             $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }
                    } else if(type === "2"){
                        var isChecked = $("#measure_" + id).is(':checked');
                        var optVal = $("#measureOptVal_" + id).val();
                        var onValue = $("#measureOptOn_" + id).val();
                        var offValue = $("#measureOptOff_" + id).val();
                        var elVal = isChecked?onValue:offValue;
                        if (optVal != elVal) {
                             $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }
                    }
                }

                if (result === false) {
                    return confirm("Value not in the optimal range. Press OK to confirm, cancel to enter new value.");
                }
                if (result) 
                   return AuthenticationForWorkOrder();
            }

            $(document).ready(function () {
                if (<%: currWO.Status%> == 5){
                    $("#bSave").attr("disabled","disabled");
                    $("input:checkbox, input:radio").click(function() {return false;});
                    $("input:text, #txtDetails").attr("readonly","readonly");
                }});
    </script>

</asp:Content>




